﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;

namespace NCKH
{
    public partial class ThongtinNNC : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

                hiendonvi();
            }
        }
        private void hiendonvi()
        {
            string sqlSelect = "SELECT MaDV,TenDV FROM DONVI ";
            string connectionString = ConfigurationManager.ConnectionStrings["NCKH"].ConnectionString;
            using (SqlConnection Cnn = new SqlConnection(connectionString))
            {
                using (SqlCommand Cmd = new SqlCommand(sqlSelect, Cnn))
                {
                    Cnn.Open();
                    using (SqlDataReader Rd = Cmd.ExecuteReader())
                    {
                        ddrmadv.Items.Insert(0, "-- Chọn đơn vị --");
                        while (Rd.Read())
                        {
                            ddrmadv.Items.Add(
                                new ListItem(Rd["TenDV"].ToString(),
                                    Rd["MaDV"].ToString()));
                        }
                    }
                    Cnn.Close();
                }
            }
        }
        protected string layMaNNC()
        {
            string connectionString = ConfigurationManager.ConnectionStrings["NCKH"].ConnectionString;
            string sqlMaNNC = "Select MaNNC From NHANGHIENCUU,TAIKHOAN Where NHANGHIENCUU.MaTK=TAIKHOAN.MaTK and Tendangnhap='" + Session["Name"].ToString() + "'";
            using (SqlConnection Cnn = new SqlConnection(connectionString))
            {
                using (SqlCommand Cmd = new SqlCommand(sqlMaNNC, Cnn))
                {
                    Cmd.CommandType = CommandType.Text;
                    using (SqlDataAdapter da = new SqlDataAdapter(Cmd))
                    {
                        DataTable tbl = new DataTable();
                        da.Fill(tbl);
                        string MaNNC = null;
                        if (tbl.Rows.Count > 0)
                        {
                            DataRow dr = tbl.Rows[0];
                            object mannc = dr["MaNNC"];
                            MaNNC = (string)mannc;

                        }
                        return MaNNC;
                    }
                }
            }
        }
        protected void SDT_Insert()
        {
            string MaNNC = layMaNNC();
            string connectionString = ConfigurationManager.ConnectionStrings["NCKH"].ConnectionString;
            using (SqlConnection Cnn = new SqlConnection(connectionString))
            {
                using (SqlCommand Cmd = new SqlCommand("SDTNNC_Insert", Cnn))
                {
                    Cmd.CommandType = CommandType.StoredProcedure;
                    Cmd.Parameters.AddWithValue("@MaNNC", MaNNC);
                    Cmd.Parameters.AddWithValue("@SDT", txtSDT.Text);
                    Cmd.Parameters.AddWithValue("@Mucuutien", 1);
                }
            }
        }
        protected void Email_Insert()
        {
            string MaNNC = layMaNNC();
            string connectionString = ConfigurationManager.ConnectionStrings["NCKH"].ConnectionString;
            using (SqlConnection Cnn = new SqlConnection(connectionString))
            { 
                using(SqlCommand Cmd = new SqlCommand("EmailNNC_Insert",Cnn))
                {
                    Cmd.CommandType=CommandType.StoredProcedure;
                    Cmd.Parameters.AddWithValue("@MaNNC",MaNNC);
                    Cmd.Parameters.AddWithValue("@Email",txtEmail.Text);
                    Cmd.Parameters.AddWithValue("@Mucuutien",1);
                }
            }
        }
        protected int layTK()
        {
            string connectionString = ConfigurationManager.ConnectionStrings["NCKH"].ConnectionString;
            string sqlMaTK = "Select MaTK From TAIKHOAN Where Tendangnhap='" + Session["Name"].ToString() + "'";
            using (SqlConnection Cnn = new SqlConnection(connectionString))
            {
                using (SqlCommand Cmd = new SqlCommand(sqlMaTK, Cnn))
                {
                    int MaTK = 0;
                    Cmd.CommandType = CommandType.Text;
                    Cnn.Open();
                    MaTK = (int)Cmd.ExecuteScalar();
                    return MaTK;
                }
            }
        }
        protected void Maquyen_Update()
        {
            string connectionString = ConfigurationManager.ConnectionStrings["NCKH"].ConnectionString;
            string sqlQuyenUpdate = "Update TAIKHOAN Set MaQ=3 Where Tendangnhap='"+Session["Name"].ToString()+"'";
            using (SqlConnection Cnn = new SqlConnection(connectionString))
            {
                using (SqlCommand Cmd = new SqlCommand(sqlQuyenUpdate, Cnn))
                {
                    Cmd.CommandType = CommandType.Text;
                    Cnn.Open();
                    Cmd.ExecuteNonQuery();
                    Cnn.Close();
                }
            }
        }
        protected void btngui_Click(object sender, EventArgs e)
        {
            int MaTK=layTK();
            string gtinh = gioitinh();
            string connectionString = ConfigurationManager.ConnectionStrings["NCKH"].ConnectionString;
            SqlConnection cnn = new SqlConnection(connectionString);
            using (SqlCommand cmd = new SqlCommand("NNC_Insert", cnn))
            {
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@MaNNC", 1);
                cmd.Parameters.AddWithValue("@TenNNC", txttennnc.Text);
                cmd.Parameters.AddWithValue("@Diachi", txtdc.Text);
                cmd.Parameters.AddWithValue("@Ngaysinh", txtns.Text);
                cmd.Parameters.AddWithValue("@Gioitinh", gtinh);
                cmd.Parameters.AddWithValue("@ChucdanhKH", ddlchucdanhkh.SelectedItem.Text);
                cmd.Parameters.AddWithValue("@Chucvu", txtcv.Text);
                cmd.Parameters.AddWithValue("@MaDV", ddrmadv.SelectedValue);
                cmd.Parameters.AddWithValue("@MaTK",MaTK);
                cmd.Parameters.AddWithValue("@CMND", txtCMND.Text);
                cnn.Open();
                cmd.ExecuteNonQuery();
                cnn.Close();
            }
            Maquyen_Update();
            Email_Insert();
            SDT_Insert();
            thongbao("Bạn đã đăng ký thành công");
            Response.Redirect("Trangchu.aspx");

        }
        private void thongbao(string Noidung)
        {

            ScriptManager.RegisterStartupScript(this, this.GetType(), "UpdateCom", "alert('" + Noidung + "');return false;", true);
        }
        protected void btnboqua_Click(object sender, EventArgs e)
        {
            txtdc.Text = txtns.Text = txttennnc.Text = txtcv.Text
                = string.Empty;
            txttennnc.Focus();
        }
        

        /*protected void cldngaysinh_SelectionChanged(object sender, EventArgs e)
        {
            txtns.Text = cldngaysinh.SelectedDate.ToShortDateString();
        }*/
        public string gioitinh()
        {
            string phai = "Nữ";
            if (rbtnnam.Checked == true)
            { phai = "Nam"; }
            return phai;
        }
    }
}